import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/apis/user_api.dart';
import 'package:sainbainu/common/models/request/pager_request.dart';
import 'package:sainbainu/common/repositories/user_repository.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/services/user.dart';
import 'package:sainbainu/common/utils/loading.dart';

import 'index.dart';

class FansController extends GetxController {
  FansController();

  final state = FansState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();

    state.refreshController = EasyRefreshController(
      controlFinishRefresh: true,
      controlFinishLoad: true,
    );
    state.userRepository = UserRepository(UserApi());
    state.pagerRequest = PagerRequest();

    getList();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.refreshController.dispose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  /// 获取内容列表
  Future getList({bool refresh = true}) async {
    if (refresh) {
      state.pagerRequest.pageNum = 1;
    }
    var res = await state.userRepository.fansList(
      state.pagerRequest,
    );
    if (refresh) {
      state.list.rows = res.rows;
    } else {
      state.list.rows.addAll(res.rows);
    }

    state.list.total = res.total;
    state.hasMore =
        state.pagerRequest.pageNum * state.pagerRequest.pageSize < res.total;
    state.pagerRequest.pageNum++;
    update(['list']);
  }

  /// 下拉刷新
  Future onRefresh() async {
    try {
      await getList();
      if (state.list.rows.isEmpty) {
        state.refreshController.finishRefresh(IndicatorResult.noMore);
      } else {
        state.refreshController.finishRefresh(IndicatorResult.success);
      }
      state.refreshController.resetFooter();
    } catch (e) {
      state.refreshController.finishRefresh(IndicatorResult.fail);
    }
  }

  /// 上拉下载
  Future onRichBottom() async {
    try {
      if (state.hasMore) {
        await getList(refresh: false);
      }
      if (!state.hasMore) {
        state.refreshController.finishLoad(IndicatorResult.noMore);
      } else {
        state.refreshController.finishLoad(IndicatorResult.success);
      }
    } catch (e) {
      state.refreshController.finishLoad(IndicatorResult.fail);
    }
  }

  /// 关注、取消关注
  void onFollowHandle(int index) {
    final item = state.list.rows[index];
    if (item.customerId == null) {
      return LoadingUtil.showError('无法操作');
    }
    if (item.toEachFlag == 1) {
      state.userRepository.unFollow(item.customerId!);
      state.list.rows[index].toEachFlag = 0;
    } else {
      state.userRepository.follow(item.customerId!);
      state.list.rows[index].toEachFlag = 1;
    }
    update(['btn']);
  }

  /// 用户详情页
  toUserDetailPage(int? customerId) {
    if (customerId == null) return;
    Get.toNamed(AppRoutes.userDetail, arguments: {'customerId': customerId});
  }
}
